﻿//----------------------------------------------------------------
// <copyright file="BLL_Resources.cs" >
//    Copyright (c) Wenzy , All rights reserved.
//    author:温正宇 E-Mail:wenzy@tom.com　MyBlog:wenzy.cnblogs.com
//    All rights reserved.
// </copyright>
//----------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using MemberShipDAL.DataObject;
using MemberShipDAL.DataObject.DataResourcesTableAdapters;
using System.Data;

namespace MemberServerBLL
{
    public class BLL_Resources
    {
        /// <summary>
        /// 获取Resources所有数据
        /// </summary>
        /// <returns></returns>
        public DataResources.wmf_ResourcesDataTable GetAllData()
        {
            DataResources.wmf_ResourcesDataTable dt = new DataResources.wmf_ResourcesDataTable();
            using (wmf_ResourcesTableAdapter da = new wmf_ResourcesTableAdapter())
            {
                da.Fill(dt);
            }
            return dt;
        }


        /// <summary>
        /// 通过资源组获得属于此资源组下面的所有资源
        /// </summary>
        /// <param name="resourcesDataTable"></param>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public DataResources.wmf_ResourcesDataTable GetAllChild(DataResources.wmf_ResourcesDataTable resourcesDataTable, string groupId)
        {
            //如果没有GroupId,就直接返回。
            if (groupId == "")
            {
                return resourcesDataTable;
            }

            //取出所有资源组
            DataResources.wmf_ResourcesGroupDataTable resourcesGroup = new BLL_ResourcesGroup().GetAllData();

            DataResources.wmf_ResourcesDataTable dt = new DataResources.wmf_ResourcesDataTable();

            DataResources.wmf_ResourcesGroupDataTable newResourcesGroup = new DataResources.wmf_ResourcesGroupDataTable();

            GetResByGroupId(resourcesGroup, resourcesDataTable, groupId, ref dt);

            return dt;
        }

        /// <summary>
        /// 把所有子结点都添加到数据集里面
        /// </summary>
        /// <param name="resourcesGroupDataTable"></param>
        /// <param name="resourcesDataTable"></param>
        /// <param name="groupId"></param>
        /// <param name="retResourcesDataTable"></param>
         private void GetResByGroupId(DataResources.wmf_ResourcesGroupDataTable resourcesGroupDataTable, DataResources.wmf_ResourcesDataTable resourcesDataTable, string groupId, ref DataResources.wmf_ResourcesDataTable retResourcesDataTable)
        {
            //找到GroupId所在的Row,添加到要返回的数据集里面
            DataView resDataView = new DataView(resourcesDataTable);
            resDataView.RowFilter = "[ResourceGroupId] = '" + groupId + "'";
            foreach (DataRowView row in resDataView)
            {
                retResourcesDataTable.ImportRow(row.Row);
            }

            //查找子结点
            DataView groupDataView = new DataView(resourcesGroupDataTable);
            groupDataView.RowFilter = "[ParentId] = '" + groupId + "'";
            foreach (DataRowView row in groupDataView)
            {
                GetResByGroupId(resourcesGroupDataTable, resourcesDataTable, row["ResourceGroupId"].ToString(), ref retResourcesDataTable);
            }
        }
    }
}
